<?php
namespace api\jeu\perso\buff;

class BuffManager implements \IteratorAggregate, \Countable {

	private $_buffs;

	public function __construct()
	{
		$this->_buffs = new \SplObjectStorage();
	}	
	
	public function attach(Buff $buff) 
	{
		$this->_buffs->attach($buff);
	}
	
	public function detach(Buff $buff) 
	{
		$this->_buffs->detach($buff);
	}
	
	public function getIterator()
	{
		return $this->_buffs;
	}
	
	public function count()
	{
		return count($this->_buffs);
	}
	
	public static function getByName($name)
	{
		
		$config = \api\arrayFromIni('buff');
		
		if(isset($config[$name]['value'])) 
		{ 
			$value = $config[$name]['value']; 
		} else { 
			$value = 1; 
		}
		
		if(isset($config[$name]['source'])) 
		{ 
			$source = $config[$name]['source']; 
		} else { 
			$source = null; 
		}
		
		if(isset($config[$name]['valuePercent'])) 
		{ 
			$valuePercent = $config[$name]['valuePercent']; 
		} else { 
			$valuePercent = false; 
		}
		
		if(isset($config[$name]['valueAbsolute'])) 
		{ 
			$valueAbsolute = $config[$name]['valueAbsolute']; 
		} else { 
			$valueAbsolute = false; 
		}
		
		if(isset($config[$name]['stack'])) 
		{ 
			$stack = $config[$name]['stack']; 
		} else { 
			$stack = 1; 
		}
		
		if(isset($config[$name]['multiplier'])) 
		{ 
			$multiplier = $config[$name]['multiplier']; 
		} else { 
			$multiplier = 1; 
		}
		
		if(isset($config[$name]['rebuffs'])) 
		{ 
			$rebuffs = $config[$name]['rebuffs']; 
		} else { 
			$rebuffs = null; 
		}
		
		return new Buff($value,$source,$valuePercent,$valueAbsolute,$stack,$multiplier,$rebuffs);
		
	}
	
}